package com.socket.nio.nio.sticky;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Hexudong
 * @date 2025/4/19
 */
@Slf4j
public class HelloWorldClient {
  public static void main(String[] args) {
    NioEventLoopGroup worker = new NioEventLoopGroup();
    try {
      Bootstrap bootstrap = new Bootstrap();
      bootstrap.group(worker);
      bootstrap.channel(NioSocketChannel.class);

      bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) {
          ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

            // 会在连接 channel 建立成功后，会触发 active 事件
            @Override
            public void channelActive(ChannelHandlerContext ctx) {
              for (int i = 0; i < 10; i++) {
                ByteBuf buf = ctx.alloc().buffer(16);
                buf.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
                ctx.writeAndFlush(buf);
              }
            }
          });
        }
      });
      ChannelFuture channelFuture = bootstrap.connect("localhost", 8082).sync();
      channelFuture.channel().closeFuture().sync();
    } catch (InterruptedException e) {
      log.error("server error", e);
    } finally {
      worker.shutdownGracefully();
    }
  }
}
